package com.sinosoft.tcmemr.webservice.dto;

import java.io.Serializable;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.sinosoft.tcmemr.webservice.constant.WebServiceConstants;
import com.sinosoft.tcmemr.webservice.util.XmlHandler;

/**
 * WebService服务响应对象
 * @author xiaoxiong
 *
 */
public class Response implements Serializable{

	private static final long serialVersionUID = 1L;

	private Header header;	//	响应头
	
	private Result result;	//	响应结果
	
	private Object content;	//	响应内容

	/**
	 * 请求处理成功构造函数
	 * @param request	请求消息
	 * @param content	回执内容
	 */
	public Response(Request request, Object content) {
		this.header = request.getHeader();
		this.result = new Result(WebServiceConstants.msg_result_success, WebServiceConstants.msg_result_success_desc);
		this.content = content;
		///this.toXmlString();
	}
	
	/**
	 * 请求处理失败构造函数
	 * @param request	请求消息
	 * @param failDesc	失败描述
	 */
	public Response(Request request, String failDesc) {
		this.header = request.getHeader();
		this.result = new Result(WebServiceConstants.msg_result_fail, failDesc);
		//this.toXmlString();
	}
	
	/**
	 * response对象转换为xml字符串
	 * @return	xml字符串
	 */
	public String toXmlString() {
		Document document = DocumentHelper.createDocument();
		Element root = document.addElement(WebServiceConstants.msg_response_root);
		root.add(XmlHandler.beanToXmlElement(this.header));
		/*if(this.content != null && this.content.size() > 0) {
			int size = this.content.size();
			for(int i=0; i<size; i++) {
				root.add(XmlHandler.beanToXmlDocumentByTemplate(content.get(i), ""));
			}
		}
		root.add(XmlHandler.beanToXmlElementByTemplate(content, ""));*/
		return document.asXML();
	}
	
	public Header getHeader() {
		return header;
	}

	public void setHeader(Header header) {
		this.header = header;
	}

	public Result getResult() {
		return result;
	}

	public void setResult(Result result) {
		this.result = result;
	}

	public Object getContent() {
		return content;
	}

	public void setContent(Object content) {
		this.content = content;
	}
	
}
